package com.pinyoumei.app.activity.ShoppingCenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivity;
import com.pinyoumei.app.activity.WebViewActivity;
import com.pinyoumei.app.adapter.MallDetailImageAdapter;
import com.pinyoumei.app.bean.PddShareBean;
import com.pinyoumei.app.bean.Photo;
import com.pinyoumei.app.bean.ShopCenterMainBean;
import com.pinyoumei.app.bean.ShopDetailBean;
import com.pinyoumei.app.bean.TbShareBean;
import com.pinyoumei.app.bean.WphDetailBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.CommonUtil;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.view.TextViewHelper.TextViewHelper;
import com.pinyoumei.app.view.dialog.BaseDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020-12-12.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-12-12        Administrator             新增Person类
 * 2020-12-12        Administrator             增加sex属性
 * 专区商品详情
 */
public class ShopItemDetailActivity extends BaseActivity implements View.OnClickListener {

    private Banner banner;
    private List<String> image_list = new ArrayList<>();
    private String goods_id = "";
    private String coupon_url = "";
    private String material_url = "";
    private MallDetailImageAdapter mallDetailImageAdapter;
    private TextView tv_select;
    private TextView tv_size;
//    private TextView tv_max_save_money;
    private TextView tv_max_save_money2;
    private TextView tv_qh_final_price;
    private TextView tv_zk_final_price;
    private TextView tv_sale_num;
    private TextView tv_title;
    private TextView tv_wph;
    private TextView tv_coupon_money;
    private TextView tv_coupon;
    private TextView tv_pre_commission;
    private ShopCenterMainBean.ResultBean.ShowGoodsListBean.GoodsListBean goodsListBean;
    private String type;
    private ShopDetailBean shopDetailBean;
    private List<String> detail_image_list;
    private RelativeLayout rl_buy;
    private WphDetailBean wphDetailBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_item_detail);


        type = getIntent().getStringExtra("type");

        goodsListBean = (ShopCenterMainBean.ResultBean.ShowGoodsListBean.GoodsListBean) getIntent().getSerializableExtra("goodsListBean");

        initWidget();

        if (goodsListBean != null) {

            coupon_url = goodsListBean.getCoupon_url();
            material_url = goodsListBean.getMaterial_url();
            goods_id = goodsListBean.getGoods_id();
//            tv_max_save_money.setText(" ¥ " + goodsListBean.getPre_commission());
            tv_qh_final_price.setText(goodsListBean.getQh_final_price());
            tv_zk_final_price.setText("¥ " + goodsListBean.getZk_final_price());
            tv_zk_final_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); //中划线
            tv_sale_num.setText("销量: " + goodsListBean.getSale_num());
            tv_coupon_money.setText(goodsListBean.getCoupon_money());
            tv_coupon.setText(goodsListBean.getCoupon_use_start_time() + " - " + goodsListBean.getCoupon_use_end_time() + " 有效");

            tv_pre_commission.setText("¥" + goodsListBean.getPre_commission());
            tv_max_save_money2.setText("¥" + goodsListBean.getMax_save_money());
            switch (type) {
                case "tb":
                    TaoBaoData();
                    tv_title.setText(TextViewHelper.setLeftImage(mContext, "tb", goodsListBean.getTitle()));
                    break;
                case "jd":
                    JDData();
                    tv_title.setText(TextViewHelper.setLeftImage(mContext, "jd", goodsListBean.getTitle()));
                    break;

                case "pdd":
                    PddData();
                    tv_title.setText(TextViewHelper.setLeftImage(mContext, "pdd", goodsListBean.getTitle()));
                    break;

                case "wph":
                    WphData();
                    tv_title.setText(TextViewHelper.setLeftImage(mContext, "wph", goodsListBean.getTitle()));
                    tv_qh_final_price.setText(goodsListBean.getZk_final_price());
                    tv_sale_num.setVisibility(View.GONE);
                    rl_buy.setVisibility(View.GONE);
                    tv_wph.setText("折扣价 ¥ ");
                    tv_zk_final_price.setText("原价 ¥ " + goodsListBean.getMarket_price());
                    tv_zk_final_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); //中划线

                    break;
                default:
                    break;
            }
        }


    }


    private void initWidget() {
        ImageView iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
//        TextView tv_go_vip = findViewById(R.id.tv_go_vip);
//        tv_go_vip.setOnClickListener(this);
        LinearLayout ll_share = findViewById(R.id.ll_share);
        ll_share.setOnClickListener(this);
        LinearLayout ll_buy = findViewById(R.id.ll_buy);
        ll_buy.setOnClickListener(this);
        rl_buy = findViewById(R.id.rl_buy);
        rl_buy.setOnClickListener(this);
        tv_select = findViewById(R.id.tv_select);
        tv_size = findViewById(R.id.tv_size);
//        tv_max_save_money = findViewById(R.id.tv_max_save_money);
        tv_max_save_money2 = findViewById(R.id.tv_max_save_money2);
        tv_qh_final_price = findViewById(R.id.tv_qh_final_price);
        tv_zk_final_price = findViewById(R.id.tv_zk_final_price);
        tv_sale_num = findViewById(R.id.tv_sale_num);
        tv_title = findViewById(R.id.tv_title);
        tv_wph = findViewById(R.id.tv_wph);
        tv_coupon_money = findViewById(R.id.tv_coupon_money);
        tv_coupon = findViewById(R.id.tv_coupon);
        tv_pre_commission = findViewById(R.id.tv_pre_commission);
        banner = findViewById(R.id.banner);

        RecyclerView rv_details_images = findViewById(R.id.rv_details_images);
        rv_details_images.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mallDetailImageAdapter = new MallDetailImageAdapter(R.layout.item_mall_detail_image, null);
        rv_details_images.setAdapter(mallDetailImageAdapter);
    }

    private void TaoBaoData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.TAOBAO_LIST_DETAIL)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setAllTypeData(response);

                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });

    }

    private void setAllTypeData(Response<String> response) {
        loadingDailog.dismiss();
        shopDetailBean = gson.fromJson(response.body(), ShopDetailBean.class);
        if (shopDetailBean.getCode() >= 0) {
            //列表
            //banner
            if (shopDetailBean.getResult().getBanner_image_list() != null && shopDetailBean.getResult().getBanner_image_list().size() > 0) {
                initBanner(shopDetailBean.getResult().getBanner_image_list());
            }

            //商品描述图片
            ArrayList<Photo> photos = new ArrayList<>();
            detail_image_list = shopDetailBean.getResult().getDetail_image_list();
            if (detail_image_list != null && detail_image_list.size() > 0) {
                for (int i = 0; i < detail_image_list.size(); i++) {
                    Photo photo = new Photo();
                    if (shopDetailBean.getResult().getDetail_image_list().get(i).contains("http")){
                        photo.setUrl(shopDetailBean.getResult().getDetail_image_list().get(i));
                    }else{
                        photo.setUrl("http:" +  shopDetailBean.getResult().getDetail_image_list().get(i));
                    }



                    photos.add(photo);
                }

                mallDetailImageAdapter.setNewData(photos);

                if (type.equals("pdd")){
                    initBanner(detail_image_list);
                }
            }


        } else {
            ToastUtils.showShort(shopDetailBean.getMsg());
        }
    }

    private void initBanner(List<String> banner_list) {
        tv_size.setText("/" + banner_list.size());
        //第一个Banner
        for (int i = 0; i < banner_list.size(); i++) {
            image_list.add(banner_list.get(i));

        }
        if (image_list.size() == 1) {
            banner.isAutoPlay(false);
        }
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置Banner的样式BannerConfig.NUM_INDICATOR
        banner.setImages(image_list); //存放图片的list集合url
        banner.setBannerAnimation(Transformer.ZoomOutSlide);//设置Banner轮播的动画特效的样式
        //banner.setPageTransformer(true,)//自定义轮播动画
        banner.setImageLoader(new GlideImageLoader());//设置图片加载器
        banner.isAutoPlay(true);//设置默认为自动轮播
        banner.setDelayTime(3000);//设置轮播间隔时间
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);

        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                String x = String.valueOf(i + 1);
                tv_select.setText(x);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        banner.start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
//            case R.id.tv_go_vip:
//                //去升级
//                startActivity(new Intent(ShopItemDetailActivity.this, SuperVipActivity.class));
//                break;
            case R.id.ll_share:
                //分享
                switch (type) {
                    case "tb":
                        TaoBaoShare(tv_pre_commission.getText().toString().trim());

                        break;
                    case "jd":

                    case "wph":
                        if (shopDetailBean != null) {
                            Intent intent = new Intent(ShopItemDetailActivity.this, ShopShareActivity.class);
                            intent.putExtra("share_price", tv_pre_commission.getText().toString().trim());
                            intent.putExtra("share_content", shopDetailBean.getResult().getShare_content());
                            intent.putExtra("share_url", shopDetailBean.getResult().getShare_url());
                            startActivity(intent);
                        }
                        break;

                    case "pdd":
                        SharePdd();
                        break;

                    default:
                        break;
                }


                break;
            case R.id.rl_buy:
            case R.id.ll_buy:
                //购买

                switch (type) {
                    case "tb":
                        BuyTaoBao();

                        break;
                    case "jd":
//                        if (shopDetailBean != null) {
//                            if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.jingdong.app.mall")) {
//
//                                CommonUtil.toJingDong(ShopItemDetailActivity.this, goods_id);
//
//
//                            } else {
//                            //没安装京东客户端
//                            CommonUtil.openBrowser(ShopItemDetailActivity.this, shopDetailBean.getResult().getShare_url());
//                        }}

                        CommonUtil.openBrowser(ShopItemDetailActivity.this, shopDetailBean.getResult().getShare_url());

                        break;

                    case "pdd":
                        if (shopDetailBean != null) {
                            BuyPdd();
                        }


                        break;

                    case "wph":
                        if (wphDetailBean != null) {
                            if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.achievo.vipshop")) {

                                CommonUtil.toWph(ShopItemDetailActivity.this, wphDetailBean.getResult().getSchema_url());


                            } else {
                                //没安装唯品会客户端
                                CommonUtil.openBrowser(ShopItemDetailActivity.this, wphDetailBean.getResult().getShare_url());
                            }
                        }
                        break;
                    default:
                        break;
                }

                break;

            default:
                break;
        }
    }



    //使用Glide加载器
    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片
            RequestOptions placeholder = new RequestOptions().placeholder(R.mipmap.ic_error_loading);
            String url = path + "";
            if (url.contains("http")) {
                Glide.with(context).load(path).apply(placeholder).into(imageView);
            } else {
                Glide.with(context).load(Constants.MAIN_URL + path).apply(placeholder).into(imageView);
            }
        }
    }

    private void TaoBaoShare(String share_price) {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.TAOBAO_GOOD_SHARE)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        TbShareBean tbShareBean = gson.fromJson(response.body(), TbShareBean.class);
                        if (tbShareBean.getCode() >= 0) {
                            //分享
                            Intent intent = new Intent(ShopItemDetailActivity.this, ShopShareActivity.class);
                            intent.putExtra("coupon_command", tbShareBean.getResult().getCoupon_command());
                            intent.putExtra("share_price", share_price);
                            intent.putExtra("share_content", tbShareBean.getResult().getShare_content());
                            intent.putExtra("share_url", tbShareBean.getResult().getShare_url());
                            startActivity(intent);
                        } else if (tbShareBean.getCode() == -1000) {
                            //未授权提示
                            BaseDialog pointOutDialog = new BaseDialog(ShopItemDetailActivity.this, R.layout.pop_point_out);
                            pointOutDialog.show();

                            Button bt_finish = pointOutDialog.findViewById(R.id.bt_finish);
                            TextView tv_point = pointOutDialog.findViewById(R.id.tv_point);
                            tv_point.setText(tbShareBean.getMsg());
                            bt_finish.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pointOutDialog.dismiss();



                                }
                            });
                        } else {
                            ToastUtils.showShort(tbShareBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }


    private void BuyTaoBao() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.TAOBAO_GOOD_SHARE)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        TbShareBean tbShareBean = gson.fromJson(response.body(), TbShareBean.class);
                        if (tbShareBean.getCode() >= 0) {
                            if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.taobao.taobao")) {
                                CommonUtil.gotoCoupon(ShopItemDetailActivity.this, tbShareBean.getResult().getShare_url());

                            } else {
                                //没安装淘宝客户端
                                CommonUtil.openBrowser(ShopItemDetailActivity.this, tbShareBean.getResult().getShare_url());
                            }


                        } else if (tbShareBean.getCode() == -1000) {
                            //未授权提示
                            BaseDialog pointOutDialog = new BaseDialog(ShopItemDetailActivity.this, R.layout.dialog_tb_shouquan);
                            pointOutDialog.show();

                            Button bt_sure = pointOutDialog.findViewById(R.id.bt_sure);
                            Button bt_cancel = pointOutDialog.findViewById(R.id.bt_cancel);
                            bt_cancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pointOutDialog.dismiss();
                                }
                            });
                            bt_sure.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pointOutDialog.dismiss();
                                    CommonUtil.openBrowser(ShopItemDetailActivity.this, tbShareBean.getResult().getAuthorization_url()+"");

//                                    if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.taobao.taobao")) {
//                                        CommonUtil.gotoCoupon(ShopItemDetailActivity.this, tbShareBean.getResult().getAuthorization_ur());
//
//                                    } else {
//                                        //没安装淘宝客户端
//                                        CommonUtil.openBrowser(ShopItemDetailActivity.this, tbShareBean.getResult().getAuthorization_ur());
//                                    }

//                                    final Intent intent = new Intent();
//                                    intent.setAction(Intent.ACTION_VIEW);
//                                    intent.setData(Uri.parse(tbShareBean.getResult().getAuthorization_ur()));

                                }
                            });
                        } else {
                            ToastUtils.showShort(tbShareBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }


    private void JDData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);
        map.put("material_url", material_url);
        map.put("coupon_url", coupon_url);


        OkGo.<String>post(Constants.JD_GOOD_DETAIL)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("material_url", material_url)
                .params("coupon_url", coupon_url)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setAllTypeData(response);

                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });

    }


    private void PddData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.PDD_GOOD_DETAIL)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setAllTypeData(response);

                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });

    }


    private void WphData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.WPH_GOOD_DETAIL)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {

                            loadingDailog.dismiss();
                        wphDetailBean = gson.fromJson(response.body(), WphDetailBean.class);
                            if (wphDetailBean.getCode() >= 0) {
                                //列表
                                //banner
                                if (wphDetailBean.getResult().getBanner_image_list() != null && wphDetailBean.getResult().getBanner_image_list().size() > 0) {
                                    initBanner(wphDetailBean.getResult().getBanner_image_list());
                                }

                                //商品描述图片
                                ArrayList<Photo> photos = new ArrayList<>();
                                detail_image_list = wphDetailBean.getResult().getDetail_image_list();
                                if (detail_image_list != null && detail_image_list.size() > 0) {
                                    for (int i = 0; i < detail_image_list.size(); i++) {
                                        Photo photo = new Photo();
                                        photo.setUrl(wphDetailBean.getResult().getDetail_image_list().get(i));
                                        photos.add(photo);
                                    }

                                    mallDetailImageAdapter.setNewData(photos);
                                }


                            } else {
                                ToastUtils.showShort(wphDetailBean.getMsg());
                            }


                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });

    }
    private void BuyPdd() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.PDD_GOOD_SHARE)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {

                        loadingDailog.dismiss();
                        PddShareBean pddShareBean = gson.fromJson(response.body(), PddShareBean.class);
                        if (pddShareBean.getCode() >= 0) {
//                            0：未授权 1：已授权
                                switch (pddShareBean.getResult().getIs_authorization()) {
                                	case 0:

                                        //未授权提示
                                        BaseDialog pointOutDialog = new BaseDialog(ShopItemDetailActivity.this, R.layout.dialog_tb_shouquan);
                                        pointOutDialog.show();

                                        TextView tv_title = pointOutDialog.findViewById(R.id.tv_title);
                                        tv_title.setText("购买及分享需要您绑定拼多多账号，且只可绑定一个拼多多账号，通过绑定的拼多多账号购物即可得到返利");
                                        Button bt_sure = pointOutDialog.findViewById(R.id.bt_sure);
                                        Button bt_cancel = pointOutDialog.findViewById(R.id.bt_cancel);
                                        bt_cancel.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                pointOutDialog.dismiss();
                                            }
                                        });
                                        bt_sure.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                pointOutDialog.dismiss();

                                                if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.xunmeng.pinduoduo")) {

                                                    CommonUtil.toPingduoduo(ShopItemDetailActivity.this, pddShareBean.getResult().getSchema_url());


                                                } else {
                                                    //没安装拼多多客户端
//                                                    CommonUtil.openBrowser(ShopItemDetailActivity.this, pddShareBean.getResult().getShare_url());

                                                    final Intent intent = new Intent(ShopItemDetailActivity.this, WebViewActivity.class);

                                                    intent.putExtra("order_title", "");
                                                    intent.putExtra("url", pddShareBean.getResult().getShare_url());
                                                   startActivity(intent);
                                                }
                                            }
                                        });


                                		break;
                                    case 1:
                                        if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.xunmeng.pinduoduo")) {

                                            CommonUtil.toPingduoduo(ShopItemDetailActivity.this, pddShareBean.getResult().getSchema_url());


                                        } else {
                                            //没安装拼多多客户端
                                            final Intent intent = new Intent(ShopItemDetailActivity.this, WebViewActivity.class);

                                            intent.putExtra("order_title", "");
                                            intent.putExtra("url", pddShareBean.getResult().getShare_url());
                                            startActivity(intent);
                                        }

                                        break;
                                	default:
                                		break;
                                }

                        } else {
                            ToastUtils.showShort(pddShareBean.getMsg());
                        }


                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }




    private void SharePdd() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this));
        map.put("goods_id", goods_id);


        OkGo.<String>post(Constants.PDD_GOOD_SHARE)
                .params("user_id",  SPUtils.getAppUserId(ShopItemDetailActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {

                        loadingDailog.dismiss();
                        PddShareBean    pddShareBean = gson.fromJson(response.body(), PddShareBean.class);
                        if (pddShareBean.getCode() >= 0){
//                            0：未授权 1：已授权
                            switch (pddShareBean.getResult().getIs_authorization()) {
                                case 0:

                                    //未授权提示
                                    BaseDialog pointOutDialog = new BaseDialog(ShopItemDetailActivity.this, R.layout.dialog_tb_shouquan);
                                    pointOutDialog.show();

                                    TextView tv_title = pointOutDialog.findViewById(R.id.tv_title);
                                    tv_title.setText("购买及分享需要您绑定拼多多账号，且只可绑定一个拼多多账号，通过绑定的拼多多账号购物即可得到返利");
                                    Button bt_sure = pointOutDialog.findViewById(R.id.bt_sure);
                                    Button bt_cancel = pointOutDialog.findViewById(R.id.bt_cancel);
                                    bt_cancel.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            pointOutDialog.dismiss();
                                        }
                                    });
                                    bt_sure.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            pointOutDialog.dismiss();

                                            if (CommonUtil.isPkgInstalled(ShopItemDetailActivity.this, "com.xunmeng.pinduoduo")) {

                                                CommonUtil.toPingduoduo(ShopItemDetailActivity.this, pddShareBean.getResult().getSchema_url());


                                            } else {
                                                //没安装拼多多客户端
//                                                    CommonUtil.openBrowser(ShopItemDetailActivity.this, pddShareBean.getResult().getShare_url());

                                                final Intent intent = new Intent(ShopItemDetailActivity.this, WebViewActivity.class);

                                                intent.putExtra("order_title", "");
                                                intent.putExtra("url", pddShareBean.getResult().getShare_url());
                                                startActivity(intent);
                                            }
                                        }
                                    });


                                    break;
                                case 1:
                                    Intent intent = new Intent(ShopItemDetailActivity.this, ShopShareActivity.class);
                                    intent.putExtra("share_price", tv_pre_commission.getText().toString().trim());
                                    intent.putExtra("share_content", pddShareBean.getResult().getShare_content());
                                    intent.putExtra("share_url", pddShareBean.getResult().getShare_url());
                                    startActivity(intent);

                                    break;
                                default:
                                    break;
                            }

                        } else {
                            ToastUtils.showShort(pddShareBean.getMsg());
                        }


                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

}
